# -*- coding: UTF-8 -*-

from .service import Service
from .common import Common


class ModelService(Service):
    _service = 'object'

    def __init__(self, base_url, timeout, dbname, username, password, uid=None):
        super().__init__(base_url, timeout)
        self.dbname = dbname
        self.username = username
        self.password = password

        if not uid:
            common = Common(base_url, timeout)
            uid = common.login(dbname, username, password)
        self.uid = uid

    def _execute(self, *args):
        method = 'execute'
        db = self.dbname
        uid = self.uid
        psw = self.password
        return self(method, db, uid, psw, *args)

    def execute(self, model, method, *args):
        # print('model, execute', model, method, args)
        return self._execute(model, method, *args)

    def _execute_kw(self, *args):
        method = 'execute_kw'
        db = self.dbname
        uid = self.uid
        psw = self.password
        return self(method, db, uid, psw, *args)

    def execute_kw(self, model, method, args, kwargs):
        # print('model, execute_kw', model, method, args, kwargs)
        return self._execute_kw(model, method, args, kwargs)
